﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPStore.Web.Services
{
    public class ValidatorProvider : System.Web.Mvc.ModelValidatorProvider
    {
        private Dictionary<Type, Type> m_ModelValidatorDictionary;

        public ValidatorProvider()
        {
            m_ModelValidatorDictionary = new Dictionary<Type, Type>();
        }

        public void AddModelValidator(Type modelType, Type modelValidatorType)
        {
            if (!m_ModelValidatorDictionary.ContainsKey(modelType))
            {
                m_ModelValidatorDictionary.Add(modelType, modelValidatorType);
            }
        }

        public override IEnumerable<System.Web.Mvc.ModelValidator> GetValidators(System.Web.Mvc.ModelMetadata metadata, System.Web.Mvc.ControllerContext context)
        {
            if (m_ModelValidatorDictionary.ContainsKey(metadata.ContainerType))
            {
                var validatorType = m_ModelValidatorDictionary[metadata.ContainerType];
                var validator = Activator.CreateInstance(validatorType, metadata, context);
                yield return validator as System.Web.Mvc.ModelValidator;
            }
        }
    }
}
